let express = require('express');
let app = express();
let whitList = ['http://localhost:3000']
app.use(function (req, res, next) {
    // 获取客户端的origin属性
    let origin = req.headers.origin;
    if (whitList.includes(origin)) {
        // 设置那个源可以访问我 必须
        res.setHeader('Access-Control-Allow-Origin', origin);
        // 允许携带哪个头访问我 
        res.setHeader('Access-Control-Allow-Headers', 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type');
        // 允许哪个方法访问我 必须
        res.setHeader('Access-Control-Allow-Methods', 'POST, GET, OPTIONS,DELETE,PUT');
        // 允许携带cookie 可选
        res.setHeader('Access-Control-Allow-Credentials', true);
        // 预检的存活时间
        res.setHeader('Access-Control-Max-Age', 6);
        // 允许返回的头
        res.setHeader('Access-Control-Expose-Headers', 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type');
        if (req.method === 'OPTIONS') {
            res.end(); //OPTIONS请求不做任何处理
        }
    }
    next();
});
app.put('/getData', function (req, res) {
    console.log(req.headers);
    res.setHeader('name', 'jw');
    res.end("我不爱你")
})
app.get('/getData', function (req, res) {
    console.log(req.headers);
    res.end("我不爱你")
})
app.use(express.static(__dirname));
app.listen(4000);